package threadUtils.demo03;

import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;

/**
 * Exchanger的阻塞情况,有bug
 */
public class ExchangerDemo02 {

    public static void main(String[] args) throws InterruptedException {
        Exchanger<String> stringExchanger = new Exchanger<>();

        Thread t1 = new Thread(() -> {
            try {
                stringExchanger.exchange("xxx");
            } catch (InterruptedException e) {
                System.out.println("an interrupted");
            }
        });
        t1.start();

        Thread t2 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                System.out.println("an interrupted time");
            }
            try {
                stringExchanger.exchange(null);
            } catch (InterruptedException e) {
                System.out.println("an interrupted");
            }
        });

        t2.start();
        TimeUnit.SECONDS.sleep(1);
        t2.interrupted();
    }

}
